<?php

namespace Modules\Agent\Exports;

use Maatwebsite\Excel\Concerns\Exportable;
use Maatwebsite\Excel\Concerns\FromCollection;
use Maatwebsite\Excel\Concerns\WithTitle;

use Maatwebsite\Excel\Concerns\WithHeadings;
use Maatwebsite\Excel\Concerns\WithMapping;
use Maatwebsite\Excel\Concerns\ShouldAutoSize;
use Maatwebsite\Excel\Concerns\WithColumnFormatting;

// use PHPExcel_Cell_DataType;
use PhpOffice\PhpSpreadsheet\Cell\DataType;

use Modules\Agent\Entities\Brokerage;

use Modules\User\Entities\User;

class BrokerageExport implements WithColumnFormatting, FromCollection, WithTitle, WithHeadings, WithMapping, ShouldAutoSize
{
    use Exportable;

    private $list;

    public function __construct($list)
    {
        $this->list = $list;
    }

    public function columnFormats(): array
    {
        return [];
        // return [
        //     'O' => DataType::TYPE_STRING
        // ];
    }

    /**
     * @return Builder
     */
    public function collection()
    {
        return $this->list;
        // return BookInfo::where('booking_session_id', $this->session->id)->displayed();
    }

    /**
     * @return string
     */
    public function title(): string
    {
        return '分销员导出';
    }

    public function map($item): array
    {

        $sku = $item->order_sku;

        $user = $item->user;

        $orderUser = $item->order_user;

        $res = [
            $item->id,
            @$item->order->number,
            @$sku->title,
            @$sku->number,
            @$sku->discount_money_price / 100,
            @$sku->total,
            $item->money / 100,
            $item->score,
            @$user->name,
            @$user->phone,
            $item->invite_level === 0 ? '直接邀请' : '间接邀请',
            @$orderUser->name,
            @$orderUser->phone,
            $item->union_status_text,
            $item->created_at
        ];

        return $res;
    }

    public function headings(): array
    {
        return [
            '唯一ID',
            '订单编号',
            '商品标题',
            '商品编号',
            '商品售价',
            '商品数量',
            '佣金(人民币)',
            '佣金(积分)',
            '受益用户',
            '受益用户手机号',
            '邀请类型',
            '下单用户',
            '下单用户手机号',
            '状态',
            '创建时间'
        ];
    }
}
